#!/bin/sh

## Note:
## After installing these fonts into CJK/LaTeX environment, you can
## use the font name as IS in your CJK/LaTeX .tex files.
## If the name of the font you installed is 'simsun.ttc', just use
## 'simsun' in your CJK/LaTeX files. you can write as follows:
##
## \documentclass[a4paper, 12pt]{article}
## \usepackage{CJK}
## \begin{CJK*}{UTF8}{simsun}
## \begin{document}
## .........
## \end{CJK*}
## \end{document}
## %_loc: _home_/bin %

## $Id: unifonts.sh 11 2007-10-06 03:09:03Z dhg $

display_usage() {
        VERSION_NUMBER=`grep "[$]Id:" $0 | cut -d ' ' -f 4`
	echo ''
        echo "unifonts revision ${VERSION_NUMBER}"
	echo ''
        echo 'Usage:'
	echo '       unifonts font_name'
	echo 'Note:'
	echo '       This is a rather simple shell script to help'
	echo '       you install a new unifont into your LaTeX/CJK'
	echo '       environment.'
	echo ''
	echo '   --------------------------------------------------------'
	echo '       !!! Be sure to run this script in the directory'
	echo '       !!! where the unifont resides. e.g, if your font'
	echo '       !!! is in /home/user/font, and this script is in'
	echo '       !!! /home/user/bin/unifonts.sh, just excute it as:'
	echo '       !!! #cd /home/user/font'
	echo '       !!! #sh /home/user/bin/unifonts.sh your_font.ttf'
	echo ''
	echo '       !!! This script has been tested on FreeBSD. Before'
	echo '       !!! executing this script please read carefully'
	echo '       !!! the settings INSIDE this script.'
	echo '   --------------------------------------------------------'
        echo ''
}

LOCALBASE=/usr/local/share
TEXMFMAIN=${LOCALBASE}/texmf
TTFONTSMAP=${TEXMFMAIN}/ttf2pk/ttfonts.map
TTFONTSDIR=${TEXMFMAIN}/fonts/truetype
TFMFONTSDIR=${TEXMFMAIN}/fonts/tfm
CJKUNDIR=${TEXMFMAIN}/tex/latex/CJK/UTF8
INSTALL_DIR="install -d -m 0755 -o root -g wheel"
INSTALL="install -m 0444 -o root -g wheel"
SLANT="0.3"
TRIMMED=
PWD=`pwd`

uf_trim_fontname() {

	for suffix in .ttf .ttc .TTF .TTC
	do
		TRIMMED=${1%$suffix}

		if [ -n ${TRIMMED} -a ${TRIMMED} != ${1} ]; then
			return;
		fi
	done

	unset TRIMMED;
}

uf_mk_fontdir() {
	if [ -e ${TTFONTSDIR} -a -w ${TTFONTSDIR} \
	     -a -e ${TFMFONTSDIR} -a -w ${TFMFONTSDIR} ]; then

		for dirname in ${TTFONTSDIR} ${TFMFONTSDIR}
		do
			cd ${dirname};
			if [ -e ./local ]; then
				cd local && ${INSTALL_DIR} ${1};
			else
				${INSTALL_DIR} ./local && cd ./local && ${INSTALL_DIR} ${1};
			fi
		done

		if [ -e ${TTFONTSDIR}/local/${1} \
		     -a -e ${TFMFONTSDIR}/local/${1} ]; then
			echo "${TTFONTSDIR}/local/${1}"
			echo "${TFMFONTSDIR}/local/${1}"
			echo "... successfully created!"
			return
		else
			echo "error creating:"
			echo "${TTFONTSDIR}/local/${1} !"
			echo "${TFMFONTSDIR}/local/${1} !"
			exit 1;
		fi
	else
		echo "you have no WRITE privilege of:"
		echo "${TTFONTSDIR}"
		echo "${TFMFONTSDIR}"
		exit 1;
	fi
}

uf_edit_file() {

	if [ -e ${TTFONTSMAP} ]; then
		if [ -w ${TTFONTSMAP} ]; then
			uf_trim_fontname ${1};
			if [ ${TRIMMED} ]; then
				FONT=${TRIMMED};
				if [ -e ${TTFONTSMAP}.orig ]; then
					echo "==========================="
					echo "${TTFONTSMAP}.orig exists!"
					echo "==========================="
				else
					cp ${TTFONTSMAP} ${TTFONTSMAP}.orig
				fi
				echo "${FONT}@Unicode@ ${1} Pid=3 Eid=1" >> ${TTFONTSMAP};
				echo "${FONT}sl@Unicode@ ${1} Pid=3 Eid=1 Slant=${SLANT}" >> ${TTFONTSMAP}
				echo "Note:"
				echo "The original"
				echo ${TTFONTSMAP}
				echo "are reserved as:"
				echo "${TTFONTSMAP}.org"
				echo "for your backup!"
				return
			fi
		else
			echo "you must be ROOT to edit:"
			echo "${TTFONTSMAP}"
			exit 1;
		fi
	else
		echo "${TTFONTSMAP} does not exist!"
		exit 1;
	fi

}

uf_mk_symlink() {

	if [ ${1} ]; then
		ORIGIN=${CWDIR}/${1}
		SYMLINK=${1}
		uf_trim_fontname ${1}
		DIRNAME=${TRIMMED}
		cd ${TTFONTSDIR}/local/${DIRNAME}
		ln -s ${ORIGIN} ${SYMLINK}
		echo "symlink created:"
		echo "${PWD}/${1} !"
	else
		echo "I can't creat the symlink:"
		exit 1;
	fi

}
	
uf_gen_tfm() {

	TTF2TFM=`which ttf2tfm`

	if [ -x ${TTF2TFM} ]; then
		uf_trim_fontname ${1}
		if [ ${TRIMMED} ]; then
			cd ${TFMFONTSDIR}/local/${TRIMMED}
			${TTF2TFM} ${TTFONTSDIR}/local/${TRIMMED}/${1} -q -P 3 -E 1 ${TRIMMED}@${TEXMFMAIN}/ttf2pk/Unicode@
			${TTF2TFM} ${TTFONTSDIR}/local/${TRIMMED}/${1} -q -P 3 -E 1 -s ${SLANT} ${TRIMMED}sl@${TEXMFMAIN}/ttf2pk/Unicode@
		else
			echo "Unkown error."
			exit 1;
		fi
	else
		echo "I can't find 'ttf2tfm' !"
		echo "Install this utility before proceeding!"
		exit 1;
	fi
}

uf_gen_fd() {

	if [ -e ${CJKUNDIR} ]; then
		FILE=c70${1}.fd
		cd ${CJKUNDIR}
		if [ -e ${FILE} ]; then
			mv ${FILE} ${FILE}.orig
		fi
		touch ${FILE}
		cat > ${FILE} << EOF
\\def\\fileversion{0.0.1}
\\def\\filedate{`date +%Y/%m/%d`}
\\ProvidesFile{${FILE}}[\\filedate\\space\\fileversion]
\\DeclareFontFamily{C70}{${1}}{\\hyphenchar \\font\m@ne}

\\DeclareFontShape{C70}{${1}}{m}{n}{<-> CJK * ${1}}{}
\\DeclareFontShape{C70}{${1}}{bx}{n}{<-> CJKb * ${1}}{\\CJKbold}
\\DeclareFontShape{C70}{${1}}{m}{sl}{<-> CJK * ${1}sl}{}
\\DeclareFontShape{C70}{${1}}{bx}{sl}{<-> CJKb * ${1}sl}{\\CJKbold}
\\DeclareFontShape{C70}{${1}}{m}{it}{<-> CJK * ${1}sl}{}
\\DeclareFontShape{C70}{${1}}{bx}{it}{<-> CJKb * ${1}sl}{\\CJKbold}
\\endinput
EOF
		echo "c70${1}.fd successfully created!"
	else
		echo "${CJKUNDIR} does not exist!"
		echo "Install CJK before proceeding!"
		exit 1
	fi
}

uf_gen_t1 () {

	TTFFILE=${1}
	uf_trim_fontname ${1}
	FONTNAME=${TRIMMED}

	CJKTTFDIR=${TTFONTSDIR}/local
	ENCDIR=${TEXMFMAIN}/fonts/enc/local/${FONTNAME}
	PFBDIR=${TEXMFMAIN}/fonts/type1/local/${FONTNAME}
	MAPDIR=${TEXMFMAIN}/fonts/map/dvips
	TYPE1_MAP=${MAPDIR}/local/local-type1.map
	PDFTEX_MAP=${MAPDIR}/local/local-pdftex.map

	${INSTALL_DIR} ${ENCDIR}
	${INSTALL_DIR} ${PFBDIR}
	${INSTALL_DIR} ${MAPDIR}/local

	# For UTF8 encoding
        PLANENUMSTART=0
        PLANENUM=255
	###################

	n=${PLANENUMSTART}

	while [ ${n} -le $PLANENUM ]
	do
  		echo -n "[$n]"
    		m=`printf %02x $n`
		SUBFONTNAME=t1${FONTNAME}${m}
		SUBFONTNAMESLANT=t1${FONTNAME}sl${m}

		ttf2pt1 -b -GaEF -pft -Ohub -W0 -l plane+pid=3,eid=1,0x$m "${CJKTTFDIR}/${FONTNAME}/${TTFFILE}" ${SUBFONTNAME}

		# Generate mapping files for updmap & PDFTeX
		fontfullname=`head -n 1 ${SUBFONTNAME}.enc | sed -e 's,^/\(.*\)Encoding.*$,\1,'`
		echo -n "${SUBFONTNAME} ${fontfullname} <${SUBFONTNAME}.pfb
${SUBFONTNAMESLANT} ${fontfullname} \" ${SLANT} SlantFont \" <${SUBFONTNAME}.pfb
" >> ${TYPE1_MAP}

		echo -n "${FONTNAME}${m} ${fontfullname} <${SUBFONTNAME}.enc <${TTFFILE}
" >> ${PDFTEX_MAP}

		${INSTALL} ${SUBFONTNAME}.pfb ${PFBDIR}
		${INSTALL} ${SUBFONTNAME}.enc ${ENCDIR}

		n=`expr $n + 1`
	done

	rm -f *.pfb *.enc local-type1.map local-pdftex.map


}

if [ -n "${1}" ]; then

	CWDIR=${PWD}
	uf_trim_fontname ${1};

	if [ ${TRIMMED} ]; then
		uf_mk_fontdir ${TRIMMED}
		uf_mk_symlink ${1}
		uf_gen_tfm ${1}
		uf_gen_t1 ${1}
		echo "============================================="
		echo ""
		uf_edit_file ${1}
		echo ""
		uf_gen_fd ${TRIMMED}
		echo  ""
		echo "============================================="
		echo ""
		echo "Now I will run 'texconfig rehash' to update"
		echo "your TEX directories. Are you sure you want"
		echo "ME to do this for you [yes/no] ?"
		read CONFIRM
		case ${CONFIRM} in
			yes)
				exec texconfig rehash
				exit 0
				;;
			no)
				echo "OK! It's up to you."
				exit 0
				;;
			*)
				echo "Well, You can do it YOURSELF."
				exit 0
				;;
		esac
	else
		echo "Is your font a TTF/TTC font?"
		exit 1;
	fi
else
	display_usage;
fi
